package com.dingqb.myapplication.paint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.util.AttributeSet;
import android.view.View;

/**
 * Create by dingqb
 * On 2019-08-20
 * Description:
 */
public class MyRegion extends View {
    public MyRegion(Context context) {
        super(context);
    }

    public MyRegion(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRegion(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //初始化画笔
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(2);

        Region rgn = new Region(10, 10, 100, 100);
        Region region = new Region(0, 0, 40, 80);
        rgn.op(region, Region.Op.INTERSECT);//交集

        drawRegion(canvas, rgn, paint);

    }

    private void drawRegion(Canvas canvas, Region rgn, Paint paint) {
        RegionIterator iter = new RegionIterator(rgn);
        Rect r = new Rect();

        while (iter.next(r)) {
            canvas.drawRect(r, paint);
        }
    }

}
